//
//  BCOrderViewManager.m
//  BangcaiSale
//
//  Created by Liang Zhang on 16/7/27.
//  Copyright © 2016年 Bangcai. All rights reserved.
//

#import "BCOrderViewManager.h"

@implementation BCOrderViewManager
+ (void)writeStuffToOrder:(NSString *)uuid {
    
    NSArray *haveStoreStuffArray = [[CoreDataStorageController sharedModel] selectElementFromEntity:@"ORDER" PredicateFromat:[NSPredicate predicateWithFormat:@"uuid == %@",uuid] SortKey:nil Limit:0 Ascending:NO];
    if (!haveStoreStuffArray.count) {
        NSArray *stuff_array = [[CoreDataStorageController sharedModel] selectElementFromEntity:@"STUFF" PredicateFromat:[NSPredicate predicateWithFormat:@"uuid == %@",uuid] SortKey:nil Limit:0 Ascending:NO];
        STUFF *stuff_model = stuff_array.lastObject;
        ORDER *model = [[CoreDataStorageController sharedModel] insertElementIntoEntity:@"ORDER"];
        model.name = stuff_model.name;
        model.category = stuff_model.category;
        model.count = @(model.count.integerValue+1);
        model.price = stuff_model.price;
        model.unit = stuff_model.unit;
        model.timestamp = [NSDate date];
        model.uuid = uuid;
    }
    else {
        ORDER *model = haveStoreStuffArray.lastObject;
        model.count = @(model.count.integerValue+1);
        model.timestamp = [NSDate date];
    }
    [[CoreDataStorageController sharedModel] saveContext];
}

+ (void)updateAddStuffToOrder:(STUFF *)stuff {
    NSArray *haveStoreStuffArray = [[CoreDataStorageController sharedModel] selectElementFromEntity:@"ORDER" PredicateFromat:[NSPredicate predicateWithFormat:@"uuid == %@",stuff.uuid] SortKey:nil Limit:0 Ascending:NO];
    if (!haveStoreStuffArray.count) {
        ORDER *model = [[CoreDataStorageController sharedModel] insertElementIntoEntity:@"ORDER"];
        model.name = stuff.name;
        model.category = stuff.category;
        model.count = @(model.count.integerValue+1);
        model.price = stuff.price;
        model.unit = stuff.unit;
        model.timestamp = [NSDate date];
        model.uuid = stuff.uuid;
    }
    else {
        ORDER *model = haveStoreStuffArray.lastObject;
        model.count = @(model.count.integerValue+1);
        model.timestamp = [NSDate date];
    }
    [[CoreDataStorageController sharedModel] saveContext];
}

+ (void)writeStuffUseCountToOrder:(NSString *)uuid Count:(NSInteger)conut {
    
    NSArray *haveStoreStuffArray = [[CoreDataStorageController sharedModel] selectElementFromEntity:@"ORDER" PredicateFromat:[NSPredicate predicateWithFormat:@"uuid == %@",uuid] SortKey:nil Limit:0 Ascending:NO];
    if (!haveStoreStuffArray.count) {
        NSArray *stuff_array = [[CoreDataStorageController sharedModel] selectElementFromEntity:@"STUFF" PredicateFromat:[NSPredicate predicateWithFormat:@"uuid == %@",uuid] SortKey:nil Limit:0 Ascending:NO];
        STUFF *stuff_model = stuff_array.lastObject;
        ORDER *model = [[CoreDataStorageController sharedModel] insertElementIntoEntity:@"ORDER"];
        model.name = stuff_model.name;
        model.category = stuff_model.category;
        model.count = @(conut);
        model.price = stuff_model.price;
        model.unit = stuff_model.unit;
        model.timestamp = [NSDate date];
        model.uuid = uuid;
    }
    else {
        ORDER *model = haveStoreStuffArray.lastObject;
        model.count = @(conut);
        model.timestamp = [NSDate date];
    }
    [[CoreDataStorageController sharedModel] saveContext];
}

+ (NSArray *)readStuffFromOrder {
    
    NSArray *orderArray = [[CoreDataStorageController sharedModel] selectElementFromEntity:@"ORDER" PredicateFromat:nil SortKey:@"timestamp" Limit:0 Ascending:NO];
    return orderArray;
}

+ (ORDER *)readStuffFromOrder:(NSString *)uuid {
    
    NSArray *orderArray = [[CoreDataStorageController sharedModel] selectElementFromEntity:@"ORDER" PredicateFromat:[NSPredicate predicateWithFormat:@"uuid == %@",uuid] SortKey:nil Limit:0 Ascending:NO];
    return orderArray.lastObject;
}

+ (NSArray *)readStuffInCategoryFromOrderWithIndex:(NSInteger)index {
    NSArray *categoryArray = [[CoreDataStorageController sharedModel] distinctElementFromEntity:@"ORDER" PredicateFromat:nil DistinctKey:@"category" SortKey:@"category"];
    NSDictionary *categoryDic = categoryArray[index];
    NSArray *orderStuffArray = [[CoreDataStorageController sharedModel] selectElementFromEntity:@"ORDER" PredicateFromat:[NSPredicate predicateWithFormat:@"category == %@",categoryDic[@"category"]] SortKey:nil Limit:0 Ascending:NO];
    return orderStuffArray;
}

+ (NSArray *)readCategoryFromOrder {
    NSArray *categoryArray = [[CoreDataStorageController sharedModel] distinctElementFromEntity:@"ORDER" PredicateFromat:nil DistinctKey:@"category" SortKey:@"category"];
    return categoryArray;
}

+ (NSInteger)readCategoryFormOrder:(NSInteger)index {
    NSArray *categoryArray = [[CoreDataStorageController sharedModel] distinctElementFromEntity:@"ORDER" PredicateFromat:nil DistinctKey:@"category" SortKey:@"category"];
    NSDictionary *categoryDic = categoryArray[index];
    NSInteger category = [categoryDic[@"category"] integerValue];
    return category;
}

+ (void)removeStuffFromOrder:(NSString *)uuid {
    NSArray *haveStoreStuffArray = [[CoreDataStorageController sharedModel] selectElementFromEntity:@"ORDER" PredicateFromat:[NSPredicate predicateWithFormat:@"uuid == %@",uuid] SortKey:nil Limit:0 Ascending:NO];
    if (haveStoreStuffArray.count) {
        ORDER *model = [haveStoreStuffArray lastObject];
        model.count = @(model.count.integerValue-1);
        if (!model.count.integerValue) {
            [[CoreDataStorageController sharedModel] removeElement:@[model]];
        }
    }
}

+ (void)updateDecStuffToOrder:(STUFF *)stuff {
    NSArray *haveStoreStuffArray = [[CoreDataStorageController sharedModel] selectElementFromEntity:@"ORDER" PredicateFromat:[NSPredicate predicateWithFormat:@"uuid == %@",stuff.uuid] SortKey:nil Limit:0 Ascending:NO];
    if (haveStoreStuffArray.count) {
        ORDER *model = [haveStoreStuffArray lastObject];
        model.count = @(model.count.integerValue-1);
        if (!model.count.integerValue) {
            [[CoreDataStorageController sharedModel] removeElement:haveStoreStuffArray];
        }
    }
}

+ (void)removeOrder:(NSString *)uuid {
    NSArray *haveStoreStuffArray = [[CoreDataStorageController sharedModel] selectElementFromEntity:@"ORDER" PredicateFromat:[NSPredicate predicateWithFormat:@"uuid == %@",uuid] SortKey:nil Limit:0 Ascending:NO];
    [[CoreDataStorageController sharedModel] removeElement:haveStoreStuffArray];
}

+ (void)removeOrderWithAarray:(NSArray *)deleteArray {
    [[CoreDataStorageController sharedModel] removeElement:deleteArray];
}

@end
